package com.edu.exam.entity.core;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 基础实体抽象类
 * 所属模块：F-07 基础数据管理模块
 * 所有业务实体都应继承此类
 *
 * @author System
 * @version 1.0.0
 */
@MappedSuperclass
@Data
@EqualsAndHashCode(of = {"id"})
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 最后修改时间
     */
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 创建者
     */
    @Column(name = "created_by", length = 100)
    private String createdBy;

    /**
     * 最后修改者
     */
    @Column(name = "updated_by", length = 100)
    private String updatedBy;

    /**
     * 版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version")
    private Long version;

    /**
     * 是否已删除
     */
    @Column(name = "deleted", nullable = false)
    private Boolean deleted = false;

    /**
     * 获取实体ID
     *
     * @return 实体ID
     */
    public Long getId() {
        return id;
    }

    /**
     * 设置实体ID
     *
     * @param id 实体ID
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * 判断是否为新实体
     *
     * @return 是否为新实体
     */
    public boolean isNew() {
        return id == null;
    }

}